package com.mall.controller;

import com.mall.common.Constant;
import com.mall.common.Result;
import com.mall.exception.MallException;
import com.mall.exception.MallExceptionEnum;
import com.mall.model.po.User;
import com.mall.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController

public class UserController {
        @Autowired
        private UserService userService;

        @PostMapping("/register")
        public Result register(@RequestParam("userName") String userName, @RequestParam("password") String password) {
                if (!StringUtils.hasText(userName)) {
//                名字找不到自定义异常
                        return Result.error(MallExceptionEnum.NEED_USERNAME);
                }
                if (!StringUtils.hasText(password)) {
                        return Result.error(MallExceptionEnum.NEED_PASSWORD);
                }
                if (password.length() < 6) {
                        return Result.error(MallExceptionEnum.NEED_PASSWORD_LENGTH);
                }
                userService.register(userName, password);
                return Result.success();
        }

        @PostMapping("/login") public Result login(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session){
                //1.数据校验
                if(!StringUtils.hasText(userName)){
                        return Result.error(MallExceptionEnum.NEED_USERNAME);
                }
                if(!StringUtils.hasText(password)){
                        return Result.error(MallExceptionEnum.NEED_PASSWORD);
                }
                //2.登录
                User user = userService.login(userName,password);
                //防止密码污染
                user.setPassword(null);
                //3.将数据存到session中
                session.setAttribute(Constant.User.CURRENT_USER,user);
                // 4.响应数据
                return Result.success(user);
                }
        //更改签名
        @PostMapping("/user/update")
        public Result update(@RequestParam("signature") String signature,HttpSession session){
                User user = (User)session.getAttribute(Constant.User.CURRENT_USER);
                if(user==null){
                        throw new MallException(MallExceptionEnum.NEED_LOGIN);
                }
                user.setPersonalizedSignature(signature);
                //修改签名
                userService.updateUser(user);
                return Result.success();
        }

        @PostMapping("/user/logout")
        public Result logout(HttpSession session){
                session.invalidate();
                return Result.success();
        }
        //后台管理员登录
        @PostMapping("/adminLogin")
        public Result adminLogin(@RequestParam("userName") String userName,
                                 @RequestParam("password") String password, HttpSession session){
                //1.数据校验
//                判断获取的username值是否为空
                if(!StringUtils.hasText(userName)){
                        return Result.error(MallExceptionEnum.NEED_USERNAME);
                }
                if(!StringUtils.hasText(password)){
                        return Result.error(MallExceptionEnum.NEED_PASSWORD);
                }
                //2.登录
                User user = userService.login(userName,password);
                //判断是否是管理员
                if (!user.getRole().equals(Constant.User.IS_ADMIN)) {
                        return Result.error(MallExceptionEnum.NEED_ADMIN);
                }
                //防止密码污染
                user.setPassword(null);
                //3.将数据存到session中
                session.setAttribute(Constant.User.CURRENT_USER,user);
                //4.响应数据
                return Result.success(user);
        }
}